<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <hello-world></hello-world>
        <click-count></click-count>
        <click-count></click-count>
        <hr/>
        <div :style='{fontSize: fontSize+"px"}'>{{pmsg}}</div>
        <ul>
            <li v-for='(item, index) in parr' :key=index>{{item +' -- ' + (index+1)}}</li>
        </ul>

        <div> ---------------- 父与子之间华丽的分割线 ---------------- </div>
        <menu-item :title='pmsg' content='hello world' :pstr='pstr' :pnum='12' pboo='true' :parr='parr' :pobj='pobj' @enlarge-text='handle'></menu-item>

        <div> ---------------- 非父子之间华丽的分割线 ---------------- </div>
        <div>父组件</div>
        <test-tom></test-tom>
        <test-jerry></test-jerry>
    </div>
    <script src="js/vue.js"></script>
    <script>
        /* 组件注册 */
        /* 组件注意事项
            - 组件参数的data值必须是函数同时这个函数要求返回一个对象 
            - 组件模板必须是单个根元素
            - 组件模板的内容可以是模板字符串
        */
        const hello = {
            data: function() {
                return {
                    msg: 'Hello world',
                }
            },
            template: '<div>{{msg}}</div>'
        };
        Vue.component('HelloWorld', hello);

        const clickCount = {
            data: function() {
                return {
                    count: 0
                }
            },
            methods: {
                click: function() {
                    ++this.count
                }
            },
            template: `
                <div>
                    <button @click='click'>点击了{{count}}次</button>
                    <hello-world></hello-world>
                </div>
            `,
        };

        /* 组件间传值 */
        // 父传子
        /* 
        :pnum='12' 数值型   pnum='12'  字符型
        bool类型也类似
        */
        const menuItem = {
            props: ['title', 'content', 'pstr', 'pnum', 'pboo', 'parr', 'pobj'],
            data: function() {
                return {
                    msg: '子组件本身的属性',
                }
            },
            methods: {
                // 不推荐，虽然可以修改值
                click: function() {
                    this.parr.push('lemon');
                },
                handle: function(event) {
                    this.$emit('enlarge-text', 5, 10);
                }
            },
            template: `
                <div>
                    {{msg + '---' + title + '----' + content}}
                    <div>pstr: {{pstr}}</div>
                    <div>pnum: {{pnum + 1}}</div>
                    <div>pboo: {{typeof pboo + ', ' + pboo}}</div>
                    <ul>
                        <li v-for='(item, index) in parr' :key=index>{{item +' -- ' + (index+1)}}</li>    
                    </ul>
                    <ul>
                        <li v-for='(item, index) in pobj' :key=index>{{item + ' -- ' + index + ' -- ' + typeof item}}</li>    
                    </ul>
                    <button @click='click'>点击</button>
                    <button @click='handle'>扩大父组件中字体大小</button>
                </div>
            `
        };

        // 非父子传值
        /* 提供事件中心 */
        const hub = new Vue();
        const testTom = {
            data: function() {
                return {
                    num: 0,
                };
            },
            template: `
                <div>
                    <div>Tom: {{num}}</div>
                    <div><button @click='handle'>点击</button></div>
                </div>
            `,
            methods: {
                handle: function() {
                    hub.$emit('jerry-event', 2);
                }
            },
            mounted: function() {
                hub.$on('tom-event', (val) => {
                    this.num += val;
                });
            }
        };

        const testJerry = {
            data: function() {
                return {
                    num: 0,
                };
            },
            template: `
                <div>
                    <div>Jerry: {{num}}</div>
                    <div><button @click='handle'>点击</button></div>
                </div>
            `,
            methods: {
                handle: function() {
                    hub.$emit('tom-event', 1);
                }
            },
            mounted: function() {
                /* hub.$on('jerry-event', (val) => {
                    this.num += val;
                }); */
                hub.$on('jerry-event', (val) => {
                    this.num += val;
                });
            }
        };

        const vue = new Vue({
            el: '#app',
            data: {
                pmsg: '父组件的内容',
                pstr: 'hello world',
                parr: ['apple', 'orange', 'banana'],
                pobj: {
                    name: 'lisi',
                    age: 18
                },
                fontSize: 10,
            },
            methods: {
                handle: function(val, val2) {
                    console.log("大~", val, val2);
                    this.fontSize += val;
                }
            },
            components: {
                'click-count': clickCount,
                'menu-item': menuItem,
                'test-tom': testTom,
                'test-jerry': testJerry,
            }
        });
    </script>
</body>

</html>